'use strict'

yjx.controller('SendCodeController', ['$rootScope', '$scope', 'ipCookie', '$timeout', '$interval', '$fetch',  'LoginService', 'modalService',
    function SendCodeController($rootScope, $scope, ipCookie, $timeout, $interval, $fetch, LoginService, modalService) {
        //console.info("RegisterController");
        var vm = this;

        //初始化变量 默认为提现获取验证码
        var APIsendCode = '/validCode/send';

        var sendText = '点击获取验证码';
        $scope.sendText = sendText;
        $scope.sendStatus = 'block';

        /**获取验证码*/
        vm.sendCode = function (phone, type) {
            $timeout(function () {
                $scope.tagStatus = true;
                $scope.sendText = '发送中..';
            });

            $fetch.fetch(
                APIsendCode, //
                {
                    params: {userId: $rootScope.userId},
                    cache: false,

                }
            ).then(function (data) {
                    //验证码已发送注意查收
                    //定时器timer
                    var timer = 120;
                    var time = timer;
                    $timeout(function () {
                        //重置定时器对象
                        typeof($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                        $scope.sendStatus = 'sending';//发送页面
                        $scope.tagStatus = true;//按钮状态 改变为disable
                        if(phone){
                            $scope.phone = phone;
                        }
                        $scope.sendText = '获取(' + time + ')';
                    });
                    $timeout(function () {
                        //定义定时器对象
                        $scope.tagTimer = $interval(function () {
                            time--;
                            if (time > 0) {
                                $scope.sendText = '获取(' + time + ')';
                            } else {
                                //time=timer;
                                $scope.tagStatus = false;
                                $scope.sendText = sendText;
                                return;
                            }
                            //console.info(time);
                        }, 1000, time);
                    });
                }).catch(function (e) {
                    $timeout(function () {
                        typeof($scope.tagTimer) == 'undefined' ? 'none' : $interval.cancel($scope.tagTimer);
                        modalService.mainMsgError(e.msg);
                        $scope.tagStatus = false;
                        $scope.sendText = sendText;
                        return;
                    });
                });
        }

    }]);